package com.dianrui.fmi.activity;

import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.dianrui.fmi.R;
import com.dianrui.fmi.adapter.QuotesAdapter;
import com.dianrui.fmi.bean.Qutos;
import com.dianrui.fmi.util.OkHttpUtils;
import com.dianrui.fmi.util.StringUtils;
import com.dianrui.fmi.util.Url;
import com.google.gson.JsonObject;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class ChooseStuckListActivity extends BaseActivity {
    private List<Qutos> mQutoList=new ArrayList<>();
    private QuotesAdapter quotesAdapter;

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.search_city)
    EditText searchCity;
    @BindView(R.id.choose_stuck_list)
    ListView chooseStuckList;

    @Override
    public int getContentViewId() {
        return R.layout.activity_choose_stuck_list;
    }

    @Override
    public void initView() {
        title.setText(getString(R.string.choose_stuck));
        quotesAdapter=new QuotesAdapter(ChooseStuckListActivity.this,mQutoList);
        chooseStuckList.setAdapter(quotesAdapter);
        getQutosList();
        quotesAdapter.setOnButtonClickListener(new QuotesAdapter.QuotesClickListener() {
            @Override
            public void onRegularClick(long tick,String name, String symbold,String type, String symbol, String fullsymbol, String price, String high, String low, String open, String zf, String yc, String buy1, String buy2, String position, String servicecharge,String zc) {
                startActivity(new Intent(ChooseStuckListActivity.this, BuySimulatedTransactionActivity.class)
                        .putExtra("symbold",symbold)
                        .putExtra("stockname",name)
                        .putExtra("price",price)
                        .putExtra("zf",zf)
                );
            }

        });
    }


    private void getQutosList() {
        JsonObject jsonObject=new JsonObject();
        jsonObject.addProperty("type","3");
        OkHttpUtils.getInstance().postWithMemberToken(Url.QUOTES_LIST, jsonObject.toString(), new OkHttpUtils.XCallBack() {
            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        final JSONObject jsonObject = new JSONObject(result);
                        if (null != jsonObject &&!isFinishing()) {
                            if ("1000".equals(jsonObject.optString("status"))) {
                                if (mQutoList != null) {
                                    mQutoList.clear();
                                }
                                JSONArray jsonArray = jsonObject.optJSONArray("data");
                                if (null != jsonArray &&jsonArray.length() > 0) {
                                    for (int i = 0; i < jsonArray.length(); i++) {
                                        Qutos Qutos = new Qutos();
                                        Qutos.symbols_id = jsonArray.optJSONObject(i).optString("symbols_id");
                                        Qutos.full_symbol = jsonArray.optJSONObject(i).optString("full_symbol");
                                        Qutos.symbol = jsonArray.optJSONObject(i).optString("symbol");
                                        Qutos.name = jsonArray.optJSONObject(i).optString("name");
                                        Qutos.type = jsonArray.optJSONObject(i).optString("type");
                                        Qutos.market = jsonArray.optJSONObject(i).optString("market");
                                        Qutos.i = jsonArray.optJSONObject(i).optString("i");
                                        Qutos.itd = jsonArray.optJSONObject(i).optString("itd");
                                        Qutos.price = jsonArray.optJSONObject(i).optString("price");
                                        Qutos.o= jsonArray.optJSONObject(i).optString("o");
                                        Qutos.h = jsonArray.optJSONObject(i).optString("h");
                                        Qutos.l = jsonArray.optJSONObject(i).optString("l");
                                        Qutos.yc= jsonArray.optJSONObject(i).optString("yc");
                                        Qutos.v= jsonArray.optJSONObject(i).optString("v");
                                        Qutos.zf= jsonArray.optJSONObject(i).optString("zf");
                                        Qutos.zd= jsonArray.optJSONObject(i).optString("zd");
                                        Qutos.tick= jsonArray.optJSONObject(i).optLong("tick");
                                        mQutoList.add(Qutos);
                                    }
                                    if (quotesAdapter != null) {
                                        quotesAdapter.refresh(mQutoList);
                                    }

                                } else {

                                }
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }


    @OnClick({R.id.back, R.id.cancel})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.cancel:
                finish();
                break;

                default:
                    break;
        }
    }

}
